// pages/device/sealInstall/sealInstall.js
import lwx from '../../../utils/lwx.js'
import {
  _globle
} from '../../../utils/globle.js'
import devices from '../../../utils/devices.js'

Page({
  _useId: 5,
  rand2: 0,
  rand1: 0,
  /**
   * 页面的初始数据
   */
  data: {
    typename: '',
    type: [],
    devicesCode: '',
    devices: {},
    devicesActId: '',
    workflow: {},
    sealName: '',
    devicesInfo: {}
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      devicesCode: options.devicesCode
    })
    this.loadData();
  },
  loadDevice: function() {
    if (!this._devices) {
      this._devices = new devices(this.data.devicesCode)
    }
    wx.showLoading({
      title: '读取设备信息',
      mask: true
    })
    this._devices.getDevicesInfo().then(res => {
      console.log(res)
      this.setData({
        devicesInfo: res
      })
      let initialised = res.initialised
      wx.hideLoading()
      wx.showLoading({
        title: '读取状态信息',
        mask: true
      })
      return this._devices.getOperationStatus()
    }).then(res => {
      console.log(res)
      wx.hideLoading()
      wx.showLoading({
        title: '读取异常信息',
        mask: true
      })
      this.rand2 = res.rand2;
      this.rand1 = res.rand1;
      // return this._devices.setOperationCommand({
      //   type: 5,
      //   sign: this.data.devices.sign,
      //   rand1: res.rand1,
      //   rand2: res.rand2
      // })
    }).then(res => {
      let statusInterval = setInterval(() => {
        this._devices.getOperationStatus().then(res => {
          if (res.operationStatus !== 1) {
            clearInterval(statusInterval)
            console.log('状态**********===='+res);
            // resolve(res.operationStatus)
          }
        })
      }, 100)
    }).then(res => {
    }).then(res => {
      wx.hideLoading()
    }).catch(err => {
      wx.hideLoading()
      wx.showToast({
        title: '读取数据失败',
        mask: true,
        icon: 'none'
      })
    })
  },
  loadData: function() {
    var that = this;
    lwx.request({
      url: "devicesInfo.info",
      data: {
        devicesCode: that.data.devicesCode
      }
    }).then(res => {
      console.log(res)
      if (res.data.code == '0') {
        that.setData({
          devices: res.data.devices
        })
        // this.loadDevice();
      }
    }).catch(err => {
      console.log('err' + err);
    })
  },
  save: function() {
    var that = this;
    lwx.request({
      url: "seal.save",
      data: {
        workflows: that.data.workflow,
        devicesCode: that.data.devicesCode,
        sealName: that.data.sealName,
        partyId: that.data.devices.partyId,
        companyId: that.data.devices.companyId,
        devicesId: that.data.devices.devicesId,
        name: that.data.devices.name
      }
    }).then(res => {
      console.log(res)
      if (res.data.code == '0') {
        wx.showToast({
          title: '提交成功',
          icon: 'none',
        })
    setTimeout(function() {
    wx.navigateTo({
      url: '../sealTypeSelect/sealTypeSelect?devicesCode=' + that.data.devicesCode + '&sign=' + that.data.devices.sign,
    })
    }, 500)
    }
    }).catch(err => {
      console.log('err' + err);
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  //流程选择
  selectType: function() {
    wx.navigateTo({
      url: '../workflowSetting/workflowSetting',
    })
  },
  sealName: function(e) {
    console.log(e)
    this.setData({
      sealName: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    if (this._devices) {
      console.log('断开连接')
      this._devices.closeConnection()
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    if (this._devices) {
      console.log('断开连接')
      this._devices.closeConnection()
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})